package org.example.library.controller;

import org.example.library.model.UserInfo;
import org.example.library.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public boolean login(String name, String password, HttpSession session){
        //账号或密码为空
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }
        UserInfo userInfo = userService.queryUserByName(name);
        //判断用户名是否存在，及用户名与密码是否正确
        if(userInfo != null && userInfo.getPassword().equals(password)) {
            userInfo.setPassword("");
            session.setAttribute("session_user_key",userInfo);
            return true;
        }
        return false;
    }
}

